<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * 贷款控制器
 */
class Loan extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->assign("action", "loan");
    }

    function index() {
        //广告轮标图
        $banner_list = $this->db->query("select banner_img,banner_link,title from tan_banner where type = 7 and status = 1 order by sort desc limit 3")->result_array();
        //推荐贷款平台
        $lend_list = $this->db->query("select id,title,logo_small,name_ch,amount_area_alisa,rate,tip_list,is_recommand from tan_lend_list where status = 1 and is_mobile=1 order by sort desc,id desc limit 10")->result_array();
        foreach ($lend_list as $key => $val) {
            $rate = ($val['rate'] * 100);
            $lend_list[$key]['rate'] = number_format($rate * 1000) % 10 == 0 ? number_format($rate, 2) . '%' : number_format($rate, 3) . '%';
            $lend_list[$key]['detail_url'] = site_url("loan/" . $val['id']);
        }
        //贷款攻略
        $lend_raiders = $this->db->query("select id,title from tan_lend_raiders where status = 1 order by sort desc limit 10")->result_array();
        foreach ($lend_raiders as $key => $val) {
            $lend_raiders[$key]['raiders_url'] = site_url("raiders/" . $val['id']);
        }
        //热门平台搜索
        $web_conf = $this->db->query("select lend_keywork from tan_website_config where id = 1 limit 1")->row_array();
        $hot_search = explode(",", $web_conf['lend_keywork']);
        //记录来源信息
        $this->in_reload_log(2);

        $key_hot_search = $hot_search[rand(0, (count($hot_search) - 1))];
        $this->assign("banner_list", $banner_list);
        $this->assign("lend_list", $lend_list);
        $this->assign("lend_raiders", $lend_raiders);
        $this->assign("hot_search", $key_hot_search);
        $this->display('loan/index.html');
    }

    function lists() {
        $records_per_page = 12;
        $sql = "select l.id,l.name_ch,l.logo_small,l.amount_area,l.rate,l.tip_list,l.is_recommand from tan_lend_list l where l.status = 1 and is_recommand=1 and is_mobile=1 order by l.sort desc,l.id desc Limit $records_per_page";
        $list = $this->db->query($sql)->result_array();
        foreach ($list as $key => $val) {
            $rate = ($val['rate'] * 100);
            $list[$key]['rate_str'] = number_format($rate * 1000) % 10 == 0 ? number_format($rate, 2) . '%' : number_format($rate, 3) . '%';
            $list[$key]['logo_small'] = strstr($val['logo_small'], "http") ? $val['logo_small'] : Constant::$WWW_HOST_URL . $val['logo_small']; //平台logo
            $list[$key]['detail_url'] = site_url("loan/" . $val['id']);
        }
        $this->assign("list", $list);
        $this->display('loan/lists.html');
    }

    function detial($lend_id) {

        if ($this->request_plat>99){
           $this->is_login();
        }

        $lend_id = $lend_id ? $lend_id : 0;
        $query_params = "id,logo_detail,name_ch,count_success,tip_list,amount_first,amount_over,period_area,rate,tip_lending_fast,tip_detail,apply_conditions,apply_materials,amount_area_arr,period_area_arr,h5_reg_url,is_mobile";
        $item = $this->db->query("select $query_params from tan_lend_list where id = $lend_id order by id desc limit 1")->row_array();
        if (!$item) {
            $this->display('deleted/index.html');
            exit;
        }
        if($item['is_mobile']==0){
            header("Location:".site_url("loan"));
            exit;
        }
        $this->db->where(array("id" => $lend_id))->set('scan_count', 'scan_count + 1', FALSE)->update("tan_lend_list");
        $rate = ($item['rate'] * 100);
        $item['rate'] = $rate * 1000 % 10 == 0 ? number_format($rate, 2) . '%' : number_format($rate, 3) . '%';
        $item['apply_conditions'] = str_replace("\n", "<br/>", $item['apply_conditions']);
        $item['apply_materials'] = str_replace("\n", "<br/>", $item['apply_materials']);
        $item['amount_area_arr'] = explode(",", $item['amount_area_arr']);
        $item['period_area_arr'] = explode(",", $item['period_area_arr']);
        $item['raiders_url'] = site_url("raiders/" . $item['id']);
        $item['contact_number'] = $this->db->query("select * from tan_reload_log where `type`=1 AND type_val = ".$lend_id)->num_rows();
        //评论
        //用户评论
        $comment_list = $this->db->query("select c.content,c.star_num,c.tags_name,c.apply_time,c.apply_amount,u.mobile,i.head_img from tan_lend_comment c left join tan_users u on u.user_id = c.user_id left join tan_users_info i on i.user_id = c.user_id where c.lend_id = $lend_id and c.is_show = 1 order by c.create_time desc limit 30")->result_array();
        foreach ($comment_list as $key => $val) {
            $comment_list[$key]['apply_time'] = date("Y-m-d", $val['apply_time']);
            $comment_list[$key]['mobile'] = mobile_formart($val['mobile']);
            $comment_list[$key]['head_img'] = strstr($val['head_img'], "http") ? $val['head_img'] : $this->img_host . $val['head_img'];
            $tag_arr = $val['tags_name'] ? explode(",", $val['tags_name']) : array();
            foreach ($tag_arr as $k => $v) {
                $temp = explode("_", $v);
                $comment_list[$key]['tags'][] = $temp[0];
            }
            if (!$tag_arr)
                $comment_list[$key]['tags'] = array();
            unset($comment_list[$key]['tags_name']);
        }

        //记录来源信息

        $this->in_reload_log(2,$lend_id);


        $this->assign("item", $item);
        $this->assign("comment_list", $comment_list);
        $this->display('loan/detail.html');
    }
    //跳转 -lht 2018.01.09
    function jumpload(){
        $request_plat = $this->session->userdata('request_plat');
        $request_flag = $this->session->userdata('request_flag');

        if ($request_plat>99){
            $this->is_login();
        }

        $lend_id = isset($_GET['lend_id']) ? trim($_GET['lend_id']) : 0;
        $plat = $request_plat ? $request_plat : trim($_GET['plat']);
        $flag = $request_flag ? $request_flag : trim($_GET['flag']);

        $item = $this->db->query("select h5_reg_url,plat_reg_url,is_mobile from tan_lend_list where id = $lend_id order by id desc limit 1")->row_array();
        $item['plat_reg_url'] = json_decode($item['plat_reg_url'],true);

        if ($item['plat_reg_url'][$plat]){
            $jump_url = $item['plat_reg_url'][$plat];
        }else{
            $jump_url = $item['h5_reg_url'];
        }

        if($item['is_mobile']==1){
            $data['type'] = 1;
            $data['type_val'] = $lend_id;
            $data['jump_url'] = $jump_url;
            $data['request_ip'] = $this->input->ip_address();
            $data['request_from'] = isset($plat)?$plat:$_SERVER['HTTP_REFERER'];
            $data['request_plat'] = $plat;
            $data['request_flag'] = $flag;
            $data['request_time'] = time();

            $this->add_reload_log($data);
        }

        header('Location: '.$jump_url);
        
        exit;
    }

    //贷款攻略详情
    function raiders($lend_id) {
        $lend_id = $lend_id ? $lend_id : 0;
        $item = $this->db->query("select r.id,r.lend_id,r.title,r.content,l.h5_reg_url from tan_lend_raiders r left join tan_lend_list l on l.id = r.lend_id where r.lend_id = " . $lend_id . " and r.status = 1 order by r.id desc limit 1")->row_array();
        if (!$item) {
            $this->display('deleted/index.html');
            exit;
        }
        $this->assign("item", $item);
        $this->display('loan/raiders.html');
    }

    //单页
    function wechat(){
        $this->display('loan/wechat.html');
    }

    //贷款平台关键词查询页面
    function search() {
        $kw = trim($_REQUEST['kw']);
        $kw_arr = explode(",", $this->website_config['lend_keywork']);
        $this->assign("kw_arr", $kw_arr);
        $this->assign("keyword", $kw);
        $this->display('loan/search.html');
    }

    //贷款平台搜索结果页面
    function search_result() {
        $kw = trim($_REQUEST['kw']);
        if ($kw) {
            $like = "and name_ch like '%$kw%'";
        } else {
            $like = "and 1 = 1";
        }
        $list = $this->db->query("select l.id,l.name_ch,l.logo_list,l.amount_area,l.rate,l.tip_list,l.is_recommand from tan_lend_list l where l.status = 1 and is_mobile=1 $like order by l.is_recommand desc,l.sort desc,l.id desc limit 12")->result_array();
        foreach ($list as $key => $val) {
            $rate = ($val['rate'] * 100);
            $list[$key]['rate_str'] = number_format($rate * 1000) % 10 == 0 ? number_format($rate, 2) . '%' : number_format($rate, 3) . '%';
            $list[$key]['logo_list'] = strstr($val['logo_list'], "http") ? $val['logo_list'] : Constant::$WWW_HOST_URL . $val['logo_list']; //平台logo
            $list[$key]['detail_url'] = site_url("loan/" . $val['id']);
        }
        $this->assign("list", $list);
        $this->assign("keyword", $kw);
        $this->display("loan/search_result.html");
    }

//贷款平台搜索结果页面
    function search_result_new() {
        $kw = trim($_REQUEST['kw']);
        if ($kw) {
            $like = " and name_ch like '%$kw%'";
        } else {
            $like = " and 1 = 1";
        }
        //增加检索
        $amount_id = trim($_REQUEST['amount_id']);
        $amount = explode(',',$amount_id);

        if ($amount_id) {
            $like .= " and amount_id in(" . $amount_id . ")";
        }
        $period_id = trim($_REQUEST['period_id']);
        if ($period_id) {
            $like .= " and period_id in(" . $period_id . ")";
        }
        $lend_id = trim($_REQUEST['lend_id']);
        if ($lend_id) {
            $like .= " and lend_id like '%" . $lend_id . "%'";
        }
        $this->assign("lend_id", $lend_id);
        //增加排序
        $orderbyin = trim($_REQUEST['orderby']);
        $orderby = "l.sort desc,l.id desc";
//        switch ($orderbyin) {
//            case "amount":
//                $orderby .= "amount_over desc,l.is_recommand asc,l.sort desc,l.id desc";
//                break;
//            case "rate":
//                $orderby .= "rate_id asc,l.is_recommand asc,l.sort desc,l.id desc";
//                break;
//            case "tip":
//                $orderby .= "tip_lending_desc asc,l.is_recommand asc,l.sort desc,l.id desc";
//                break;
//            default :
//                $orderby .= "l.is_recommand asc,l.sort desc,l.id desc,l.is_recommand asc,l.sort desc,l.id desc";
//        }
        if($lend_id){
            $default_type = $this->db->query("select id,title from tan_lend_category where id = $lend_id limit 1")->row_array();

            $this->assign("default_type",$default_type);
        }

        $amount_type = $this->db->query("select id,title from tan_lend_category where pid=1")->result_array();
        $this->assign("amount_type", $amount_type);
        $period_type = $this->db->query("select id,title from tan_lend_category where pid=3")->result_array();
        $this->assign("period_type", $period_type);
        $lend_type = $this->db->query("select id,title from tan_lend_category where pid=17")->result_array();
        $this->assign("lend_type", $lend_type);

        $list = $this->db->query("select l.id,l.name_ch,l.logo_small,l.amount_area,l.rate,l.tip_list,l.is_recommand from tan_lend_list l where l.status = 1 and is_mobile=1 $like order by $orderby limit 0,12")->result_array();
        foreach ($list as $key => $val) {
            $rate = ($val['rate'] * 100);
            $list[$key]['rate_str'] = number_format($rate * 1000) % 10 == 0 ? number_format($rate, 2) . '%' : number_format($rate, 3) . '%';
//            $list[$key]['logo_small'] = strstr($val['logo_small'], "http") ? $val['logo_small'] : Constant::$WWW_HOST_URL . $val['logo_small']; //平台logo
            $list[$key]['detail_url'] = site_url("loan/" . $val['id']);
        }
        $this->assign("list", $list);
        
        $this->assign("keyword", $kw);
        $this->display("loan/search_result_new.html");
    }

    //是否登录
    private function is_login(){
        if(!$this->user_id){
            $url = site_url("account/reg").$this->get_next_request_url();
            header("Location:".$url);
            exit;
        }
    }
}
